$(function() {
	platformId = "";// 当前页面所使用的平台id
	getPlatformId();// 
	initBtmMessageSelect();
	
	//数据初始值
	getBTMLocalStorageInit();
	//监听数据变化，存储本地数据
	addListenerEventFun();
	
	addNumberKeyboard();
});

function getPlatformId() {// 从测试序列界面的链接路径中截取ID
	var url = location.search; // 获取url中"?"符后的字串
	path = location.host;
	if (url.indexOf("?") != -1) {
		var str = url.substr(1);
		strs = str.split("&");
		platformId = strs[3].split("=")[1];
	}
}

tests=function(moudle){ // BTM页面数据发送功能
 	var inputs=$("#top input");//拿到所有的输入框
 	var selects=$("#top select");//拿到所有的下拉框
	if(moudle=="start"){//点击的是开始和终止按钮
		console.log("开始测试");
		var str=getDate(inputs,selects);
		ajaxSendBTMInfo(str,1);
	}
	else if(moudle=="stop"){
		console.log("停止测试");
		var str=getDate(inputs,selects);
		ajaxSendBTMInfo(str,3);
	}
	else if(moudle=="updated"){
		console.log("更新数据");
		var str=getDate(inputs,selects);
		console.log(str);
		ajaxSendBTMInfo(str,2);
	}	
 };
 
function getDate(inputs,selects){
		var str='{';
		for(var i=0;i<inputs.length;i++){//获取Input的键值
     	var input = $(inputs[i]);
     	var key =input.attr("name");
    		var value =input.val();
     	str += key+':"'+value+'",';
     }
     for(var i=0;i<selects.length;i++){//获取select的键值
     	var select = $(selects[i]);
     	var key =select.attr("name");
    		var value =select.find("option:selected").attr("signalValue"); 
    		if(selects.length-1==i){
				str += key+':"'+value+'"}';
     	}else{
     		str += key+':"'+value+'",';
     	}
     }
     return str;
}

function ajaxSendBTMInfo(str,sendSignal) {//外围设备信息发送功能
	addLoading();
	$.ajax({
		"url" : "../../../btm/ajaxSendBTMSignal",
		"type" : "post",
		"data" : {
			"sendSignal" : sendSignal,
			"platformId" : platformId,
			"str" : str
		},
		"success" : function(result) {
			removeLoading();
			if (result.flag == 0) {
				var bean = result.content;
				beanToStr(bean);
				console.log("发送成功");
				book('发送成功');
			} else {
				var content=result.errorInfo;
				book(content);
			}
		},
		"error" : function() {
			removeLoading();
			var content="手工测试连接错误！";
			book(content);
		}
	});
}

function initBtmMessageSelect(){
	$.ajax({
		url:"../../../btm/getBtmMessageList",
		type:"post",
		success:function(data){
			var flag = data.flag;
			if(flag==0){
				var content =data.content;
				for(var i=0;i<content.length;i++){
					$("#BTMMessage").append("<option signalValue='"+content[i].ID+"'>"+content[i].MESSAGE_NAME+"</option>");
				}
			}else{
				var content ="<div style='color:black;'>获取报文列表异常！</div>";
				warning(content);
			}
		},
	error:function(){
		var content ="<div style='color:black;'>获取报文列表异常！</div>";
		warning(content);
	}
	});
}
function beanToStr(bean){
	 if(bean.lkjReqSwithCode!=null){
		 $("#lkjReqSwithCode").text(bean.lkjReqSwithCode);
	 }
	 if(bean.transponderNum!=null){
		 $("#transponderNum").text(bean.transponderNum);
	 }
	 if(bean.speed!=null){
		 $("#speeds").text(bean.speed);
	 }
	 if(bean.distance!=null){
		 $("#distance").text(bean.distance);
	 }
	 if(bean.time!=null){
		 $("#time").text(bean.time);
	 }
	 if(bean.lkjDate!=null){
		 $("#lkjDate").text(bean.lkjDate);
	 }
}

function addListenerEventFun(){
	//监听数据变化，存储本地数据
	$("input").on('input',function(e)  
	{  
		switch (e.target.name){
			case 'Interval':
				localStorage.setItem('BTMInterval', e.target.value);
				break;
			case 'LastingTime':
				localStorage.setItem('BTMLastingTime', e.target.value);
				break;
			default:
				break;
		}
	}); 
	
	//监听数据变化，存储本地数据
	$("select").on('change',function(e){
		console.log(e);
		switch (e.target.name){
			case 'BTMType':
				localStorage.setItem('BTMBTMType', e.target.selectedIndex);
				break;
			case 'DatagramNums':
				localStorage.setItem('BTMDatagramNums', e.target.selectedIndex);
				break;
			case 'SendingMode':
				localStorage.setItem('BTMSendingMode', e.target.selectedIndex);
				break;
			case 'Status':
				localStorage.setItem('BTMStatus', e.target.selectedIndex);
				break;
			case 'DatagramNo':
				localStorage.setItem('BTMDatagramNo', e.target.selectedIndex);
				break;
			default:
				break;
		}
	});
}
//读取存储数据BTM数据
function getBTMLocalStorageInit(){
	if(localStorage.getItem('BTMInterval')){
		$("input[name='Interval']").val(localStorage.getItem('BTMInterval'));
	}
	if(localStorage.getItem('BTMLastingTime')){
		$("input[name='LastingTime']").val(localStorage.getItem('BTMLastingTime'));
	}
	
	if(localStorage.getItem('BTMBTMType')){
		$("select[name = 'BTMType']").get(0).selectedIndex = localStorage.getItem('BTMBTMType');
	}
	if(localStorage.getItem('BTMDatagramNums')){
		$("select[name = 'DatagramNums']").get(0).selectedIndex = localStorage.getItem('BTMDatagramNums');
	}
	if(localStorage.getItem('BTMSendingMode')){
		$("select[name = 'SendingMode']").get(0).selectedIndex = localStorage.getItem('BTMSendingMode');
	}
	if(localStorage.getItem('BTMStatus')){
		$("select[name = 'Status']").get(0).selectedIndex = localStorage.getItem('BTMStatus');
	}
	if(localStorage.getItem('BTMDatagramNo')){
		$("select[name = 'DatagramNo']").get(0).selectedIndex = localStorage.getItem('BTMDatagramNo');
	}
}

//添加数字软键盘
function addNumberKeyboard(){
	if($(".numkeyboard").length>0){
		$(".numkeyboard").numkeyboard({
			keyboardRadix: 600, //键盘大小基数
			mainbackground: '#313439', //主背景色
			menubackground: '#313439', //头背景色
			exitbackground: '#4376A0', //关闭按钮背景色
			buttonbackground: 'transparent', //键盘背景色
			clickeve: true, //是否绑定元素click事件
		});
	}
}